new Vue({
  el: '#app',
  data() {
    return {
      form: {
        Applicable_Type: "",
        Warehouse_Type: "",
        page: 1,			//页数
        limit: 10			//每页数据条数
      },
      total: 0,
      tableData: [],		//表格数据
    }
  },
  mounted() {
    this.getList(this.form)
  },
  methods: {
    onSerach() {			//查询方法
      this.form.page = 1;
      this.getList(this.form)
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
      this.getList(this.form)
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form)
    },
    handleChildren(row) {
      const _this = this
      $.ajax({
        url: `${DomainName}/getWarehouseInventoryID?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Warehouse_SerialNumber: row.Applicable_Type == '0' ? row.Warehouse_SerialNumber : row.Parent_Warehouse_SerialNumber
        },
        success: function (res) {
          console.log(row);
          row.Children.warehouse_inventory = res
        },
        dataType: "json"
      });
    },
    getList(data) {     //换页方法
      const _this = this
      $.ajax({
        type: 'POST',
        url: `${DomainName}/getWarehouseAllAction?token=${$.cookie('token')}`,
        data,
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          if (Result.Status == 'success') {
            _this.tableData = Result.tableData
            _this.total = parseInt(Result.limit)
            console.log(_this.tableData);
          }
        },
        dataType: "json"
      });
    },
    addEnterprise() {
      window.location.href = './Add.html'
    },
    editMember(Warehouse_SerialNumber) {
      sessionStorage.setItem("Warehouse_SerialNumber", Warehouse_SerialNumber
      )
      window.location.href = './Edit.html'
    },
    //点击按钮
    deletMember(Warehouse_SerialNumber) {
      const _this = this;
      this.$confirm('此操作将删除该仓库, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        _this.get_original_value(Warehouse_SerialNumber, (obj) => {
          _this.Delete(obj);
        });
        // _this.Delete(Warehouse_SerialNumber);
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除'
        });
      });
    },
    get_original_value(Primary_Key, callback) {
      console.log(Primary_Key);
      let _this = this
      $.ajax({
        url: DomainName + '/getSubtableInquiryOriginal?token=' + $.cookie("token"),
        type: 'POST',
        data: {
          JSONObject: JSON.stringify({
            warehouse_info: { warehouse_inventory: {} }
          }),
          Primary_Key: Primary_Key
        },
        success: function (res) {
          callback(res.warehouse_info[0])
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    //删除
    Delete(row) {
      const _this = this;
      let rowData = [];
      // 判断 row 的类型
      if (Array.isArray(row)) {
        // 如果是数组，则取第一个元素作为 rowData
        rowData = [...row];
      } else {
        // 否则，直接使用 row 作为 rowData
        rowData.push(
          row
        );
      }
      rowData.forEach((item) => {
        item.Row_Status = '1'
      })
      let data = {
        Data: JSON.stringify({
          Modify: { warehouse_info: rowData }
        })
      };
      console.log(rowData);
      $.ajax({
        url: DomainName + '/getEditAction?token=' + $.cookie('token'),
        type: 'POST',
        data,
        error: function (array) {
          console.log(array);
          _this.$message.error("删除失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (res) {
          if (res.Status == 'success') {
            _this.$message({
              type: 'success',
              message: '删除成功!'
            });
            _this.getList(_this.form)
          }
        }
      })
    },
    GoWareDetail(row) {
      sessionStorage.setItem('Initialization',JSON.stringify(row))
      window.location.href = './Initialization.html'
    }
  },
})